package emd24.telstudyresources;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * This class represents the quiz thaat the user is taking. It contains a
 * list of Questions and keeps track of which question the user is on. 
 * 
 * @author Evan Dyke
 *
 */
public class Quiz {
	private ArrayList<Question> questions;
	private int currQuestionIndex;
	
	/**
	 * Creates an empty quiz.
	 * 
	 */
	public Quiz(){
		questions = new ArrayList<Question>();
	}
	public Quiz(JSONObject js) throws JSONException{
		int length = js.getInt("length");
		questions = new ArrayList<Question>();
		JSONArray questions = js.getJSONArray("questions");
		for(int q = 0; q < questions.length(); q++){
			this.questions.add(new Question(questions.getJSONObject(q)));
		}
	}
	
	/**
	 * Adds a Question to the end of the quiz.
	 * 
	 * @param q Question to add to the quiz.
	 */
	public void addQuestion(Question q){
		questions.add(q);
	}
	
	/**
	 * Gets the question at a particular index.
	 * 
	 * @param index int of index to get question at (starting at 0).
	 * @return Question at index position
	 */
	public Question getQuestionAt(int index){
		return questions.get(index);
	}
	/**
	 * Gets the current question in the quiz.
	 * 
	 * @return the current question.
	 */
	public Question getCurrentQuestion(){
		return questions.get(this.getCurrQuestionIndex());
	}
	
	/**
	 * Gets the number of questions in the quiz.
	 * 
	 * @return number of questions in the quiz
	 */
	public int getNumQuestions(){
		return questions.size();
	}
	
	/**
	 * Gets the index of the current question.
	 * 
	 * @return the index of the current question
	 */
	public int getCurrQuestionIndex() {
		return currQuestionIndex;
	}
	
	/**
	 * Sets the index of the current question
	 * 
	 * @param currQuestionIndex the index of the current question to set
	 */
	public void setCurrQuestionIndex(int currQuestionIndex) {
		this.currQuestionIndex = currQuestionIndex;
	}
}
